﻿using System;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

namespace Netkk.Core
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer container;

        public UnityControllerFactory(IUnityContainer container)
        {
            this.container = container;
        }

        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(0x194, "No Controller Found");
            }

            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException("TypeDoesNotSubclassControllerBase", "controllerType");
            }

            return container.Resolve(controllerType) as IController;
        }
    }
}